package com.example.demo.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.example.demo.utils.pay.ConstantPropertiesUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Service
public class OssUtil {
	private ConstantPropertiesUtils constantPropertiesUtils;

	public OssUtil(ConstantPropertiesUtils constantPropertiesUtils) {
		this.constantPropertiesUtils = constantPropertiesUtils;
	}

	public String uploadFileAvatar(MultipartFile file) {
		//获取oss上传配置文件中的参数
		String bucketName = constantPropertiesUtils.getBucketname();
		String endpoint = constantPropertiesUtils.getEndpoint();
		String keyId = constantPropertiesUtils.getKeyid();
		String keySecret = constantPropertiesUtils.getKeysecret();

		OSS ossClient;
		InputStream inputStream;
		try {
			// 创建OSSClient实例。
			ossClient  = new OSSClientBuilder().build(endpoint, keyId, keySecret);
			// 上传文件流
			inputStream = file.getInputStream();

			//为了使得文件可以重复上传，每次上传的时候需要将文件名进行修改
			String fileName = file.getOriginalFilename();

			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			String newFileName = uuid + fileName;

			//获取当前日期,然后以日期和新的文件名组成全路径，使得oss中的文件按照日期进行分类存储
			String date = new DateTime().toString("yyyy/MM/dd");
			String fullFileName = date + "/" + newFileName;


			//第一个参数Bucket名称 第二个参数 上传到oss文件路径和文件名称
			ossClient.putObject(bucketName, fullFileName, inputStream);

			// 关闭OSSClient。
			ossClient.shutdown();
			return "https://"+bucketName+"."+ endpoint+"/"+fullFileName;
		} catch (Exception e) {
//			log.error("文件上传失败",e);
//			throw new ProtagonistException(StatusCode.REMOTEERROR,"文件上传oss失败");
			e.printStackTrace();
			return null;
		}
	}


}
